package com.rundow.common.sdk.factory;

import cn.hutool.core.util.StrUtil;
import com.rundow.common.base.exception.NotFoundException;
import com.rundow.common.sdk.bean.driver.ServiceContainerContext;
import com.rundow.common.sdk.service.DriverCommandService;
import com.rundow.common.sdk.service.DriverCustomService;
import com.rundow.common.sdk.service.DriverScheduleService;
import com.rundow.driver.modbus.factory.ModBusFactory;
import com.rundow.driver.opc.da.factory.OpcDAFactory;
import com.rundow.driver.opc.ua.factory.OpcUAFactory;
import com.rundow.driver.plcs7.factory.PlcS7Factory;
import com.rundow.driver.virtual.factory.VirtualFactory;

public interface ServiceContainerFactory {
    DriverCustomService createDriverCustomService();

    DriverCommandService createdriverCommandService();

    DriverScheduleService createdriverScheduleService();

    default ServiceContainerFactory driverFactory(String driverName) {
        if (StrUtil.isBlank(driverName)) {
            throw new NullPointerException("driverName is nulll ");
        }
        if (StrUtil.equals(driverName, "OpcUaDriver")) {
            return new OpcUAFactory();
        } else if (StrUtil.equals(driverName, "OpcDaDriver")) {
            return new OpcDAFactory();
        } else if (StrUtil.equals(driverName, "ModbusDriver")) {
            return new ModBusFactory();
        } else if (StrUtil.equals(driverName, "PlcS7Driver")) {
            return new PlcS7Factory();
        } else if (StrUtil.equals(driverName, "VirtualDriver")) {
            return new VirtualFactory();
        } else {
            throw new NotFoundException("not found this driver {} Factory", driverName);
        }
    }

    default ServiceContainerContext.ServiceContainer createServiceContainer(String driverName) {
        final ServiceContainerFactory serviceContainerFactory = driverFactory(driverName);
        DriverCustomService driverCustomService = serviceContainerFactory.createDriverCustomService();
        DriverCommandService driverCommandService = serviceContainerFactory.createdriverCommandService();
        DriverScheduleService driverScheduleService = serviceContainerFactory.createdriverScheduleService();
        ServiceContainerContext.ServiceContainer serviceContainer = new ServiceContainerContext.ServiceContainer(driverCustomService, driverCommandService, driverScheduleService);
        return serviceContainer;
    }
}
